package com.rinvay.asdownloader.core.network;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.rinvay.asdownloader.core.Core;
import com.rinvay.asdownloader.core.access.NetworkAccess;
import com.rinvay.asdownloader.platform.AndroidFactory;
import com.rinvay.utils.logger.Log;
import com.rinvaylabs.utils.IpAddressUtils;

public class NetworkManager
{
    
    private static final String TAG = "AccessManager";
    
    private Core core;
    
    private static NetworkManager instance;
    
    private ConnectivityManager connectivityMgr;
    
    /**
     * 当前的网络连接
     */
    private NetworkAccess curAccess;
    
    public synchronized static void createInstance(Core core) {
        if(instance == null) {
            instance = new NetworkManager(core);
        }
    }
    
    public static NetworkManager getInstance() {
        return instance;
    }
    
    private NetworkManager(Core core) {
        this.core = core;
        connectivityMgr = (ConnectivityManager) AndroidFactory.getApplicationContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
    }
    
    public synchronized void start() {
        Log.i(TAG, "start access manager ");
     // Register network state listener
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        AndroidFactory.getApplicationContext().registerReceiver(networkStateListener,
                intentFilter);

    }
    
    public synchronized void stop() {
        Log.i(TAG, "stop access manager ");
        AndroidFactory.getApplicationContext().unregisterReceiver(networkStateListener);
    }
    

    private synchronized void connectionEvent(String action) {
        Log.i(TAG, "connection Event " + action);
        if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
            NetworkInfo networkInfo = connectivityMgr.getActiveNetworkInfo();
            if(networkInfo == null) {
                Log.i(TAG, "Disconnected:no network (e.g. air plane mode)");
                return;
            }
            
            String localIP = IpAddressUtils.getLocalIpAddress();
            Log.i(TAG, "local ip address is " + localIP);
        }
    }

    
    
    /**
     * Network state listener
     */
    private BroadcastReceiver networkStateListener = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, final Intent intent) {
            Thread t = new Thread() {
                public void run() {
//                    Iterator<String> it = intent.getExtras().keySet().iterator();
//                    while(it.hasNext()) {
//                        String key = it.next();
//                        Log.i(innerTag, key);
//                    }
                    connectionEvent(intent.getAction());
                }

            };
            t.start();
        }
    };


}
